// 通知类型定义
export interface Notification {
  id: string;
  title: string;
  content: string;
  level: 'school' | 'grade' | 'class'; // 全校、年级、班级
  targetId?: string; // 目标ID（年级ID或班级ID）
  publisher: string;
  publishTime: string;
  isImportant: boolean;
  status: 'draft' | 'published' | 'expired';
}

// 通知阅读状态
export interface NotificationReadStatus {
  notificationId: string;
  userId: string;
  readTime: string;
}

// 模拟通知数据
export const mockNotifications: Notification[] = [
  {
    id: 'N001',
    title: '关于新学期开学的通知',
    content: '亲爱的同学们，新学期将于9月1日正式开始，请大家按时返校报到。具体安排请查看附件。',
    level: 'school',
    publisher: '校长办公室',
    publishTime: '2024-08-20 10:00:00',
    isImportant: true,
    status: 'published'
  },
  {
    id: 'N002',
    title: '高一年级家长会通知',
    content: '高一年级将于9月15日下午2点召开家长会，请各位家长准时参加。',
    level: 'grade',
    targetId: 'G001', // 高一年级ID
    publisher: '高一年级组',
    publishTime: '2024-09-05 14:30:00',
    isImportant: true,
    status: 'published'
  },
  {
    id: 'N003',
    title: '高一(1)班班会通知',
    content: '高一(1)班将于本周三下午第三节课召开班会，主题是新学期规划。',
    level: 'class',
    targetId: 'C001', // 高一(1)班ID
    publisher: '班主任-张老师',
    publishTime: '2024-09-08 09:15:00',
    isImportant: false,
    status: 'published'
  },
  {
    id: 'N004',
    title: '校园安全讲座通知',
    content: '为增强学生安全意识，学校将于9月20日举办校园安全讲座，全体学生必须参加。',
    level: 'school',
    publisher: '学生处',
    publishTime: '2024-09-10 16:20:00',
    isImportant: true,
    status: 'published'
  },
  {
    id: 'N005',
    title: '期中考试安排',
    content: '期中考试将于11月15日-11月17日进行，请各年级做好复习准备。',
    level: 'school',
    publisher: '教务处',
    publishTime: '2024-10-25 11:00:00',
    isImportant: true,
    status: 'draft'
  }
];

// 模拟通知阅读状态数据
export const mockNotificationReadStatuses: NotificationReadStatus[] = [
  {
    notificationId: 'N001',
    userId: 'U001',
    readTime: '2024-08-20 10:30:00'
  },
  {
    notificationId: 'N001',
    userId: 'U002',
    readTime: '2024-08-20 11:15:00'
  },
  {
    notificationId: 'N002',
    userId: 'U001',
    readTime: '2024-09-05 15:00:00'
  }
];

// API函数
export const getNotifications = async (): Promise<Notification[]> => {
  // 模拟API延迟
  await new Promise(resolve => setTimeout(resolve, 300));
  return mockNotifications.filter(n => n.status === 'published');
};

export const getNotificationById = async (id: string): Promise<Notification | undefined> => {
  // 模拟API延迟
  await new Promise(resolve => setTimeout(resolve, 200));
  return mockNotifications.find(n => n.id === id);
};

export const createNotification = async (notification: Omit<Notification, 'id' | 'publishTime'>): Promise<Notification> => {
  // 模拟API延迟
  await new Promise(resolve => setTimeout(resolve, 400));
  const newNotification: Notification = {
    ...notification,
    id: `N${String(mockNotifications.length + 1).padStart(3, '0')}`,
    publishTime: new Date().toISOString().replace('T', ' ').substring(0, 19)
  };
  mockNotifications.push(newNotification);
  return newNotification;
};

export const updateNotification = async (id: string, updates: Partial<Notification>): Promise<Notification> => {
  // 模拟API延迟
  await new Promise(resolve => setTimeout(resolve, 300));
  const index = mockNotifications.findIndex(n => n.id === id);
  if (index === -1) {
    throw new Error('通知不存在');
  }
  mockNotifications[index] = { ...mockNotifications[index], ...updates };
  return mockNotifications[index];
};

export const deleteNotification = async (id: string): Promise<boolean> => {
  // 模拟API延迟
  await new Promise(resolve => setTimeout(resolve, 200));
  const index = mockNotifications.findIndex(n => n.id === id);
  if (index === -1) {
    return false;
  }
  mockNotifications.splice(index, 1);
  return true;
};

export const getReadStatuses = async (notificationId?: string, userId?: string): Promise<NotificationReadStatus[]> => {
  // 模拟API延迟
  await new Promise(resolve => setTimeout(resolve, 200));
  let result = [...mockNotificationReadStatuses];
  if (notificationId) {
    result = result.filter(status => status.notificationId === notificationId);
  }
  if (userId) {
    result = result.filter(status => status.userId === userId);
  }
  return result;
};

export const markAsRead = async (notificationId: string, userId: string): Promise<NotificationReadStatus> => {
  // 模拟API延迟
  await new Promise(resolve => setTimeout(resolve, 100));
  const existingStatus = mockNotificationReadStatuses.find(
    status => status.notificationId === notificationId && status.userId === userId
  );
  
  if (existingStatus) {
    return existingStatus;
  }
  
  const newStatus: NotificationReadStatus = {
    notificationId,
    userId,
    readTime: new Date().toISOString().replace('T', ' ').substring(0, 19)
  };
  mockNotificationReadStatuses.push(newStatus);
  return newStatus;
};

// 获取未读通知数量
export const getUnreadCount = async (userId: string): Promise<number> => {
  // 模拟API延迟
  await new Promise(resolve => setTimeout(resolve, 100));
  const publishedNotifications = mockNotifications.filter(n => n.status === 'published');
  const readStatuses = mockNotificationReadStatuses.filter(status => status.userId === userId);
  const readNotificationIds = new Set(readStatuses.map(status => status.notificationId));
  return publishedNotifications.filter(n => !readNotificationIds.has(n.id)).length;
};